<?php if(!$this->inline) { ?><div class="grid_15"><?php } ?>
<script type="text/javascript">
$(document).ready(function() {
	$('#instructorSearchField').autocomplete({
		source : rd.getBaseUrl() + 'json/autocomplete/instructororproxy/',
		minLength : 2,
		select : function(event,ui) {
			this.value = ui.item.label;
			$('#searchInstructorResult').val(ui.item.value);
			//this.form.submit();
			return false;
		},
		change : function() {
			if ('' == $.trim($('#instructorSearchField').val())) {
				$('#searchInstructorResult').val('');
			}
		}
	});
	$('#departmentSearchField').autocomplete({
		source : rd.getBaseUrl() + 'json/autocomplete/dept/',
		minLength : 2,
		select : function(event,ui) {
			this.value = ui.item.label;
			$('#searchDepartmentResult').val(ui.item.value);
			//this.form.submit();
			return false;
		}
	});
	$('#courseSearchField').autocomplete({
		source : rd.getBaseUrl() + 'json/autocomplete/course/',
		minLength : 2,
		select : function(event,ui) {
			this.value = ui.item.label;
			$('#searchCourseResult').val(ui.item.value);
			//this.form.submit();
			return false;
		}
	});
});
</script>
<?php 
	$instructorErrorClass = Rd_Layout::hasMessage('instructorMatchError') ? ' error' : '';
	$departmentErrorClass = Rd_Layout::hasMessage('departmentMatchError') ? ' error' : '';
	$courseErrorClass = Rd_Layout::hasMessage('courseMatchError') ? ' error' : '';

	if ('' != trim($this->msg)) {?>
	<div  class="helperText"><?php print($this->msg); ?></div>
<?php } ?>

<?php if(!$this->inline) { ?><div class="cancelNavigation">[ <a href="<?php print( $this->returnUrl ? $this->returnUrl : "index.php?cmd={$this->returnCommand}"); ?>">Cancel</a> ]</div><?php } ?>

<?php if ($this->showSearch) {
	$defaultTerm = (
		$this->termValue
		? $this->termValue
		: Terms::getCurrentTerm()->getId()
	);
	$instructorFieldValue = (
		$this->instructorFieldValue
		? "value=\"$this->instructorFieldValue\""
		: ''
	);
	$instructorValue = (
		$this->instructorValue
		? "value=\"$this->instructorValue\""
		: ''
	);
	$departmentFieldValue = (
		$this->departmentFieldValue
		? "value=\"$this->departmentFieldValue\""
		: ''
	);
	$courseFieldValue = (
		$this->courseFieldValue
		? "value=\"$this->courseFieldValue\""
		: ''
	);	
	
	?>
<h2 class="formHeader grid_5" id="coursePickerHeader"><?php print( $this->formTitle ? $this->formTitle : 'SEARCH COURSES');?></h2>
<form class="bordered clearing basicPadding bottomMargin" action="./" method="get" name="searchCourse">
	<input type="hidden" name="cmd" value="<?php print(htmlentities($this->formCommand)); ?>" />
	<?php if ($this->hiddenFields && is_array($this->hiddenFields)) {
		$this->getDisplayer()->displayHiddenFields($this->hiddenFields);
	} ?>
	<div class="fullWidth">
		<label class="blockLabel bottomHalfMargin"><span class="labelText">Term: </span>
			<?php $this->getDisplayer()->renderTermSelect(array(
				'fieldName' => 'searchTerm',
				'defaultTerm' => $defaultTerm
			)); ?>
		</label>
		<label class="blockLabel bottomHalfMargin<?php print($instructorErrorClass); ?>">
			<span class="labelText">Instructor: </span>
			<input type="text" name="searchInstructor" id="instructorSearchField" <?php print($instructorFieldValue); ?> size="40" />
			<span class="supplementText">Name or Username</span>
			<?php if ($instructorErrorClass) { ?>	
				<span class="formAlertMessage supplementText failedText"><?php print(Rd_Layout::getMessage('instructorMatchError'));?></span>
			<?php } ?>
		</label>
		<input type="hidden" name="searchInstructorResult" id="searchInstructorResult" <?php print($instructorValue); ?>/>
		<label class="blockLabel bottomHalfMargin<?php print($departmentErrorClass); ?>">
			<span class="labelText">Department: </span>
			<input type="text" name="searchDepartment" id="departmentSearchField" <?php print($departmentFieldValue); ?> size="40" />
			<span class="supplementText">Course Department Name or Abbreviation</span>
			<?php if ($departmentErrorClass) { ?>	
				<span class="formAlertMessage supplementText failedText"><?php print(Rd_Layout::getMessage('departmentMatchError'));?></span>
			<?php } ?>
		</label>
		<!-- <input type="hidden" name="searchDepartmentResult" id="searchDepartmentResult"/> -->
		<label class="blockLabel bottomHalfMargin<?php print($courseErrorClass); ?>">
			<span class="labelText">Course: </span>
			<input type="text" name="searchCourse" id="courseSearchField" <?php print($courseFieldValue); ?> size="40" />
			<span class="supplementText">Course Number or Course Name</span>
			<?php if ($courseErrorClass) { ?>	
				<span class="formAlertMessage supplementText failedText"><?php print(Rd_Layout::getMessage('courseMatchError'));?></span>
			<?php } ?>
		</label>
		<!-- <input type="hidden" name="searchCourseResult" id="searchCourseResult"/> -->
		<input type="submit" name="searchCourseSubmit" value="Search Courses" />
		<?php if ($this->returnUrl) { ?><input type="hidden" name="referer" value="<? print(htmlentities($this->returnUrl)); ?>" /><?php } ?>
	</div>
	<div class="clear"></div>
</form>
</div>
<?php } ?>
<?php if (is_array($this->courses) && count($this->courses) > 0) { ?>
<h2 class="formHeader grid_5" id="coursePickerHeader">
	<?php print($this->resultsTitle ? $this->resultsTitle : ($this->showSearch ? 'MATCHING COURSES' : 'AVAILABLE COURSES'));?> 
	(<?php print(count($this->courses)); ?>)
</h2>
<div id="selectCourse" class="bordered clearing bottomMargin">
	<table class="pickerResults fullWidth"">
		<tr class="headingRow">
			<th>Term</th>	
			<th>Course</th>
			<th>Details</th>
			<th width="25%">Instructor(s)</th>
			<?php if (Account_Rd::atLeastProxy()) { ?><td class="headerLikeCell"></td><?php } ?>
		</tr>
<?php 
		$rowClass = 'evenRow';
		$otherButtonParams = (
			$this->returnUrl
			? '&referer=' . urlencode($this->returnUrl)
			: ''
		);
		$otherButtonParams .= (
			$this->item
			? '&item=' . urlencode($this->item)
			: ''
		);
		//loop through the courses
		foreach($this->courses as $course)
		{
			$rowClass = ($rowClass == 'evenRow') ? 'oddRow' : 'evenRow';
			$disabled = 
				(
					$this->disabledCourseInstances 
					&& is_array($this->disabledCourseInstances)
					&& in_array($course->getCourseInstanceId(), $this->disabledCourseInstances)
				) || (
					$this->disabledCourseAliases 
					&& is_array($this->disabledCourseAliases)
					&& in_array($course->getCourseAliasId(), $this->disabledCourseAliases)
				);
			
			$coursePreviewLink = Account_Rd::atLeastStaff()
				? './?cmd=editClass&ci=' . $course->getCourseInstanceId()
				: './?cmd=viewReservesList&ci=' . $course->getCourseInstanceId();
?>
				<tr class="<?php print($rowClass);?>">
					
					<td><?php  print($course->getTermLabel()); ?></td>
					<td class="centerContents">
						<?php 
						if (!$disabled) {
							$linkParamArray = 
								'ca' == strtolower($this->linkOn)
								? array('ca' => $course->getCourseAliasId())
								: array('ci' => $course->getCourseInstanceId()); 
							if ($this->buttonParams) {
								foreach($this->buttonParams as $param => $value) {
									$linkParamArray[$param] = $value; // we want these to appear in the order prodived, always after the ci/ca
								}
							}
							if('post' == strtolower($this->method)) {
						?>
							<form action="./" method="post">
								<input type="hidden" name="cmd" value="<?php print($this->buttonCommand ? $this->buttonCommand : $this->formCommand); ?>"/>
							<?php 
								foreach($linkParamArray as $param => $value) { ?>
								<input type="hidden" name="<?php print($param); ?>" value="<?php print(htmlentities($value)); ?>"/>
							<?php } ?>
								<input type="submit" class="noWrap buttonLink alignCenter" value="<?php  print(($this->buttonLabelPrefix ? $this->buttonLabelPrefix : '') . $course->getStandardLabel()); ?>"/>
							</form>
						<?php 
							} else {
								$linkParamsComponents = array();
								foreach($linkParamArray as $param => $value) {
									$linkParamsComponents[] = $param . '=' . urlencode($value);
								}
								$linkParams = htmlentities(implode('&', $linkParamsComponents));
						?>
						<a class="noWrap buttonLink alignCenter" href="./?cmd=<?php print($this->buttonCommand ? $this->buttonCommand : $this->formCommand); ?><?php print('' != $linkParams ? "&{$linkParams}" : '');?>"><?php  print(($this->buttonLabelPrefix ? $this->buttonLabelPrefix : '') . $course->getStandardLabel()); ?></a>
						<?php 
							} ?>
						<?php 
						} else { ?><?php  print($course->getStandardLabel() . ' ' . ($this->diabledButtonHint ? $this->diabledButtonHint : '')); ?><?php } ?>
					</td>
					<td>
						<?php  if ($course->hasStudentById(Account_Rd::getId())) { ?>
						<img class="icon altTooltip" alt="Enrolled Course" src="<?php Rd_Layout::printIconUrl('book-open')?>"/>
						<?php } ?>
						
						<?php  if (Account_Rd::lessThanStaff()) { 
							if ($course->isOpenEnrollment()) {?>
						<img class="icon altTooltip" alt="Enrolled Course" src="<?php Rd_Layout::printIconUrl('book_globe')?>"/>
							<?php } else { ?>
						<img class="icon altTooltip" alt="Enrolled Course" src="<?php Rd_Layout::printIconUrl('book_lock')?>"/>
						<?php 
								}
							} ?>
						
						<?php if ($course->isInUse() && Account_Rd::atLeastProxy()) { ?>
						<img class="icon altTooltip" alt="Active Course (in use)" src="<?php Rd_Layout::printIconUrl('book-brown')?>"/>
						<?php } else if ($course->isNew() && Account_Rd::atLeastProxy()) { ?>
						<img class="icon altTooltip" alt="New Course (not in use)" src="<?php Rd_Layout::printIconUrl('book_new')?>"/>
						<?php } else if (Account_Rd::atLeastProxy()) { ?>
						<img class="icon altTooltip" alt="Canceled/Inactive Course (not in use)" src="<?php Rd_Layout::printIconUrl('book_exclamation')?>"/>
						<?php  }
						 
							if ($course->isManuallyCreated() && Account_Rd::atLeastStaff()) {?>
						<img class="icon altTooltip" alt="Manually Created Course (not created or managed by registrar)" src="<?php Rd_Layout::printIconUrl('book_user')?>"/>
						<?php } if ($course->hasRequests() && Account_Rd::atLeastStaff()) { ?>
						<img class="icon altTooltip" alt="Course has unfinished requests" src="<?php Rd_Layout::printIconUrl('book_question')?>"/>
						<?php } if ($course->needsCopyrightApproval() && Account_Rd::atLeastStaff()) { ?>
						<img class="icon altTooltip" alt="Course needs copyright review" src="<?php Rd_Layout::printIconUrl('book_error')?>"/>
						<?php } 
						
							print($course->getName()); 
							if ($course->getCrossListingLabel()) { ?>
							<br/><span class="crossListingLabel">(Cross-listed as: 
							<?php  if ($course->getCourseInstance()->getPrimaryCourse()->hasStudentById(Account_Rd::getId())) { ?>
								<img class="icon altTooltip" alt="Enrolled Course" src="<?php Rd_Layout::printIconUrl('book-open')?>"/>
							<?php } ?>	
							<?php print($course->getCrossListingLabel());?>)
							</span>
							<?php } else if ($this->labelCrossListingParents && $course->isInCrossListing()) { ?>
								<br/><span class="crossListingLabel">(Cross-listed with other courses)</span>							
							<?php } ?></td>
					<td><?php  print($course->getInstructorList()); ?></td>
					<?php if (Account_Rd::atLeastProxy()) { ?>
					<td><a class="previewLink" href="<?php print($coursePreviewLink); ?>" target="_previewCourse"><span class="linkIcon"></span>Preview</a></td>
					<?php } ?>
				</tr>
<?php 
		}
?>
			</table>
	<div class="clear"></div>
	<div class="iconLegend"><ul>
		<?php if (Account_Rd::atLeastProxy()) { ?>
		<li class="horizontal grid_4">
			<ul>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book-brown')?>"/> = active courses</li>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_exclamation')?>"/> = new courses not in use</li>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_new')?>"/> = canceled/inactive courses</li>
			</ul>
		</li>
		<?php } ?>
		<li class="horizontal grid_4">
			<ul>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book-open')?>"/> = enrolled courses</li>
				<?php if (Account_Rd::lessThanStaff()) {?>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_globe')?>"/> = open courses</li>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_lock')?>"/> = closed courses</li>				 
				 <?php } ?>
			</ul>
		</li>
		<?php if (Account_Rd::atLeastStaff()) { ?>
		<li class="horizontal grid_4">
			<ul>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_user')?>"/> = manually created courses</li>
				<!-- #TODO finish these labels in #2.4 <li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_question')?>"/> = courses with unfinished requests</li>
				<li><img class="icon" src="<?php Rd_Layout::printIconUrl('book_error')?>"/> = courses needing copyright review</li> -->
			</ul>
		</li>
		<?php } ?>
	</ul></div>
	<div class="clear"></div>
<?php if(!$this->inline) { ?></div> <?php } ?>
<?php  } else if ($this->noResultsMessage) { ?>
	<h2 class="formHeader grid_5" id="coursePickerHeader">
	<?php print($this->resultsTitle ? $this->resultsTitle : ($this->showSearch ? 'MATCHING COURSES' : 'AVAILABLE COURSES'));?> 
	(<?php print(count($this->courses)); ?>)
	</h2>
	<div id="selectCourse" class="basicPadding bordered clearing bottomMargin"><?php print($this->noResultsMessage);?></div>
<?php } ?>